using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Practices.EnterpriseLibrary.Validation.Properties;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration;


namespace BussinessLogicTecnoPez
{
    #region "MoneyValidator"
    [ConfigurationElementType(typeof(CustomValidatorData))]
    public class TecnoPezMoneyValidator : Validator<string>
    {

        public TecnoPezMoneyValidator(String templateMessage)
            : base(templateMessage,null)
        {
            
        }

        protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            double d;
            if(!Double.TryParse(objectToValidate,out d)){
                loguearNoValidacion(currentTarget, key, validationResults);
            }
            else if (d < 0)
            {
                loguearNoValidacion(currentTarget, key, validationResults);
            }
        }

        private void loguearNoValidacion(object currentTarget, string key, ValidationResults validationResults)
        {
            string message = this.MessageTemplate;
            this.LogValidationResult(validationResults, message, currentTarget, key);
        }

        protected override string DefaultMessageTemplate
        {
            get { return "No es una moneda valida."; }
        }
    }

    public class TecnoPezMoneyValidatorAttribute : ValidatorAttribute
    {
        protected override Validator DoCreateValidator(Type targetType)
        {
            return new TecnoPezMoneyValidator(this.Mensaje);
        }
        private string Mensaje;
        public TecnoPezMoneyValidatorAttribute(string mensaje)
        {
            Mensaje = mensaje;
        }
    }
    #endregion

    #region "No empty string"
    [ConfigurationElementType(typeof(CustomValidatorData))]
    public class TecnoPezNoEmptyStringValidator : Validator<string>
    {
        private string myMessage;
        public TecnoPezNoEmptyStringValidator(String templateMessage)
            : base(templateMessage, null)
        {
            myMessage = templateMessage;
        }

        protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            if (string.IsNullOrEmpty(objectToValidate))
            {
                string message = this.MessageTemplate;
                this.LogValidationResult(validationResults, message, currentTarget, key);
            }
        }

        protected override string DefaultMessageTemplate
        {
            get { return myMessage; }
        }
    }

    public class TecnoPezNoEmptyStringValidatorAttribute : ValidatorAttribute
    {
        protected override Validator DoCreateValidator(Type targetType)
        {
            return new TecnoPezNoEmptyStringValidator(this.Mensaje);
        }
        private string Mensaje;
        public TecnoPezNoEmptyStringValidatorAttribute(string mensaje)
        {
            Mensaje = mensaje;
        }
    }
    #endregion

    #region "validador dobles positivos"
    [ConfigurationElementType(typeof(CustomValidatorData))]
    public class TecnoPezPositiveDouble: Validator<double>
    {
        private string myMessage;
        public TecnoPezPositiveDouble(String templateMessage)
            : base(templateMessage, null)
        {
            myMessage = templateMessage;
        }

        protected override void DoValidate(double objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            if (objectToValidate<0)
            {
                string message = this.MessageTemplate;
                this.LogValidationResult(validationResults, message, currentTarget, key);
            }
        }

        protected override string DefaultMessageTemplate
        {
            get { return myMessage; }
        }
    }

    public class TecnoPezPositiveDoubleAttribute : ValidatorAttribute
    {
        protected override Validator DoCreateValidator(Type targetType)
        {
            return new TecnoPezPositiveDouble(this.Mensaje);
        }
        private string Mensaje;
        public TecnoPezPositiveDoubleAttribute(string mensaje)
        {
            Mensaje = mensaje;
        }
    }
    #endregion

    #region "validador no nulos"
    [ConfigurationElementType(typeof(CustomValidatorData))]
    public class TecnoPezNoNullValidator : Validator<string>
    {
        private string myMessage;
        public TecnoPezNoNullValidator(String templateMessage)
            : base(templateMessage, null)
        {
            myMessage = templateMessage;
        }

        protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            if (objectToValidate == null)
            {
                string message = this.MessageTemplate;
                this.LogValidationResult(validationResults, message, currentTarget, key);
            }
        }

        protected override void DoValidate(object objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            if (objectToValidate == null)
            {
                string message = this.MessageTemplate;
                this.LogValidationResult(validationResults, message, currentTarget, key);
            }
        }


        protected override string DefaultMessageTemplate
        {
            get { return myMessage; }
        }
    }

    public class TecnoPezNoNullValidatorAttribute : ValidatorAttribute
    {
        protected override Validator DoCreateValidator(Type targetType)
        {
            return new TecnoPezNoNullValidator(this.Mensaje);
        }
        private string Mensaje;
        public TecnoPezNoNullValidatorAttribute(string mensaje)
        {
            Mensaje = mensaje;
        }
    }
    #endregion

    #region "Valid Decimal"
    [ConfigurationElementType(typeof(CustomValidatorData))]
    public class TecnoPezValidDecimalValidator : Validator<decimal>
    {
        private string myMessage;
        public TecnoPezValidDecimalValidator(String templateMessage)
            : base(templateMessage, null)
        {
            myMessage = templateMessage;
        }

        protected override void DoValidate(decimal objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            if (objectToValidate == -1)
            {
                string message = this.MessageTemplate;
                this.LogValidationResult(validationResults, message, currentTarget, key);
            }
        }

        protected override string DefaultMessageTemplate
        {
            get { return myMessage; }
        }
    }

    public class TecnoPezValidDecimalValidatorAttribute : ValidatorAttribute
    {
        protected override Validator DoCreateValidator(Type targetType)
        {
            return new TecnoPezValidDecimalValidator(this.Mensaje);
        }
        private string Mensaje;
        public TecnoPezValidDecimalValidatorAttribute(string mensaje)
        {
            Mensaje = mensaje;
        }
    }
    #endregion
}
